package com.example.LibrarySystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.LibrarySystem.Exception.ServiceException;
import com.example.LibrarySystem.annotation.RoleAnnotation;
import com.example.LibrarySystem.mapper.BorrowingMapper;
import com.example.LibrarySystem.mapper.InventoryMapper;
import com.example.LibrarySystem.pojo.Borrowing;
import com.example.LibrarySystem.pojo.Inventory;
import com.example.LibrarySystem.response.Result;
import com.example.LibrarySystem.service.BorrowingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/borrowing")
@Transactional
public class BorrowingController {

    @Autowired
    private BorrowingService borrowingService;

    @Autowired
    private BorrowingMapper borrowingMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    /**
     * 借书
     * @param borrowing
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Borrowing borrowing) {
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_id",borrowing.getBookId());
        // 判断一下当前借的书还有没有库存
        Inventory inventory = inventoryMapper.selectOne(queryWrapper);
        if (inventory.getNumber() <= 0) {
            return new Result("书本已被借完", -100);
        }

        borrowing.setBorroDate(new Date());
        borrowing.setReturn(false);
        borrowing.setStatus(0);
        int res = borrowingMapper.insert(borrowing);
        // 库存减1
        int i = inventoryMapper.updateNumberByMinus(borrowing.getBookId());
        if (res + i > 0) {
            return new Result("借书成功", 200);
        }
        return new Result("借书失败请重试", -1);
    }

    /**
     * 查询借书信息
     *
     * @param data
     * @return
     */
    @GetMapping("/getBorrowByInfo")
    public Result getBorrowByInfo(@RequestParam Map<String, String> data) {
        return borrowingService.getBorrowByInfo(data);
    }

    /**
     * 修改借书信息(还书)
     */
    @PostMapping("/update")
    public Result update(@RequestBody Borrowing borrowing) {
        borrowing.setReturnDate(new Date());
        borrowing.setReturn(true);
        QueryWrapper<Borrowing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", borrowing.getId());
        int res = borrowingMapper.update(borrowing, queryWrapper);

        int i = inventoryMapper.updateNumberByAdd(borrowing.getId());
        Result result = new Result();
        if (res + i > 0) {
            result.setMsg("修改成功");
            result.setStatus(200);
        } else {
            result.setMsg("修改失败");
            result.setStatus(-1);
        }
        return result;
    }

    /**
     * 批量还书
     */
    @PostMapping("/returnBooks")
    public Result returnBooks(@RequestBody int[] ids) {
        try {
            int res = 0;
            int length = ids.length;
            for (int i = 0; i < length; i++) {
                res += inventoryMapper.updateNumberByIdAndAdd(ids[i]);
                res += borrowingMapper.returnBooks(ids[i], new Date());
            }
            return new Result("添加成功", 200);
        } catch (Exception e) {
            throw new ServiceException("-1", "添加失败");
        }

    }

    /**
     * 统计本周有多少本书被借出去
     */
    @GetMapping("/week")
    public Result week(){
        List<Map<String, Object>> week = borrowingService.week();
        Result result = new Result();
        result.put("data",week);
        result.setMsg("查询成功");
        result.setStatus(200);
        return result;
    }

    /**
     * 统计本月有多少本书被借出去
     */
    @GetMapping("/month")
    public Result month(){
        List<Map<String, Object>> month = borrowingService.month();
        Result result = new Result();
        result.put("data",month);
        result.setMsg("查询成功");
        result.setStatus(200);
        return result;
    }

}
